package com.wct.spzx.manager.controller;

import com.wct.spzx.manager.service.SecureService;
import com.wct.spzx.manager.service.SysUserService;
import com.wct.spzx.manager.service.ValidateCodeService;
import com.wct.spzx.model.dto.system.LoginDto;
import com.wct.spzx.model.entity.system.SysUser;
import com.wct.spzx.model.entity.user.UserInfo;
import com.wct.spzx.model.vo.common.Result;
import com.wct.spzx.model.vo.common.ResultCodeEnum;
import com.wct.spzx.model.vo.system.LoginVo;
import com.wct.spzx.model.vo.system.ValidateCodeVo;
import com.wct.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.java.Log;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@Log
@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private ValidateCodeService validateCodeService;

    @Resource
    private SecureService secureService;

    @Operation(summary = "获取验证码")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取公钥")
    @GetMapping(value = "/getPublicKey")
    public Result<String> getPublicKey() throws Exception{
        return Result.build(secureService.getPublicKey(), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        log.info("loginDto: " + loginDto);
        LoginVo loginVo = sysUserService.login(loginDto);
        // 记录登录日志
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo() {
        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
    }


//    @Operation(summary = "获取当前登录用户信息")
//    @GetMapping(value = "/getUserInfo")
//    public Result<UserInfo> getUserInfo(@RequestHeader("token") String token) {
//        //1 从请求头获取token
////        String token = request.getHeader("token");
//        //2 根据token查询redis获取用户信息
//        UserInfo userInfo = sysUserService.getUserInfo(token);
////        log.info("token: " + token);
////        log.info("userInfo: " + userInfo.toString());
//        //3 返回用户信息
//        return Result.build(userInfo, ResultCodeEnum.SUCCESS);
//    }

    @Operation(summary = "用户退出")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader("token") String token) {
        //1 从请求头获取token
//        String token = request.getHeader("token");
        //2 根据token在redis中删除用户信息
        sysUserService.logout(token);
//        log.info("token: " + token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
